// Copyright 2008 Senary
// See credits.txt for details.

// This file is part of Omega VII.

// Omega VII is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// Omega VII is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with Omega VII.  If not, see <http://www.gnu.org/licenses/>.

#include <utility>
#include "PlanetPanel.h"
#include "Player.h"
#include "Planet.h"
#include "GameManager.h"
#include "Inputmanager.h"

using namespace std;

PlanetPanel::PlanetPanel(void)	
		:factoryButton(make_pair(850, 350), 2, "factory.png"),
		minesButton(make_pair(920, 350), 2, "mine.png"),
		spaceships1Button(make_pair(850, 420), 2, "yataiviii.png"),
		spaceships2Button(make_pair(920, 420), 2, "jonth.png"),
		spaceships3Button(make_pair(850, 490), 2, "cysus.png"),
		spaceships4Button(make_pair(920, 490), 2, "methi.png"),
		_shield(_VAR(0)), _factoryLand(_VAR(0)), _maxFactoryLand(_VAR(0)),
		_mineLand(_VAR(0)), _maxMineLand(_VAR(0)), _constructionPoint(_VAR(0))
{
	_panelDefinition += "digits(64, 128, \"Shield       : %4.0f\", *, 1, 0);\n";

	_panelDefinition += "digits(64, 160, \"Factory land : %5.0f\", *, 1, 1);\n";
	_panelDefinition += "digits(192, 160, \"/ %5.0f\", *, 1, 2);\n";

	_panelDefinition += "digits(64, 192, \"Mines        : %5.0f\", *, 1, 3);\n";
	_panelDefinition += "digits(192, 192, \"/ %5.0f\", *, 1, 4);\n";

	_panelDefinition += "digits(64, 224, \"Construction Points : %5.0f\", *, 1, 5);\n";

	InputManager* im = GameManager::getInstance()->getInputManager();

	//register buttons with the input manager
	im->registerButton(&factoryButton);
	im->registerButton(&minesButton);
	im->registerButton(&spaceships1Button);
	im->registerButton(&spaceships2Button);
	im->registerButton(&spaceships3Button);
	im->registerButton(&spaceships4Button);

	//observe their on_click events
	factoryButton.addObserver(this);
	minesButton.addObserver(this);
	spaceships1Button.addObserver(this);
	spaceships2Button.addObserver(this);
	spaceships3Button.addObserver(this);
	spaceships4Button.addObserver(this);

	factoryButton.setToolTipText("Factory:120");
	minesButton.setToolTipText("Mine:90");
	spaceships1Button.setToolTipText("Yatai VIII:460");
	spaceships2Button.setToolTipText("Jonth:1000");
	spaceships3Button.setToolTipText("Cysus:1030");
	spaceships4Button.setToolTipText("Methi:1670");
}

PlanetPanel::~PlanetPanel(void)
{
}

// param timestep: time since the last update, in seconds
// param totalTime: total time the game has been running
void PlanetPanel::update(float timestep, float totalTime)
{
	if (_observed != 0) {
		Planet* p = dynamic_cast<Planet*>(_observed);

		_shield = _VAR(p->getShield());
		_maxFactoryLand = _VAR(p->getFreeLand());
		_maxMineLand = _VAR(p->getResourceLand());
		_constructionPoint = _VAR(p->getConstructionPoint());

		//cheat enemy's planet value
		if(p->getOwner() && !p->getOwner()->getIsHumanPlayer()) {
			_factoryLand = _VAR(1000);
			_mineLand = _VAR(900);
		}
		else {
			_factoryLand = _VAR(p->getFactoriesLand());
			_mineLand = _VAR(p->getMinesLand());
		}
	}
}

//create PANEL useable by engine
void PlanetPanel::createPanel()
{
	RightPanel::createPanel();
	digits_set(_pPanel, _VAR(1), &_shield);
	digits_set(_pPanel, _VAR(2), &_factoryLand);
	digits_set(_pPanel, _VAR(3), &_maxFactoryLand);
	digits_set(_pPanel, _VAR(4), &_mineLand);
	digits_set(_pPanel, _VAR(5), &_maxMineLand);
	digits_set(_pPanel, _VAR(6), &_constructionPoint);

	factoryButton.createPanel();
	minesButton.createPanel();
	spaceships1Button.createPanel();
	spaceships2Button.createPanel();
	spaceships3Button.createPanel();
	spaceships4Button.createPanel();
}

// Makes the panel visible
void PlanetPanel::display()
{
	Planet* planet = dynamic_cast<Planet*>(_observed);
	if(planet==0) return;

	ImagePanel::display();
	_icon.display();

	//Omega7 does not have owner
	if(planet->getOwner()==0) return;

	//hide buttons if planet does not belong to player
	if(planet->getOwner()->getIsHumanPlayer()) {
		factoryButton.display();
		minesButton.display();
		spaceships1Button.display();
		spaceships2Button.display();
		spaceships3Button.display();
		spaceships4Button.display();
	}
	else {
		factoryButton.hide();
		minesButton.hide();
		spaceships1Button.hide();
		spaceships2Button.hide();
		spaceships3Button.hide();
		spaceships4Button.hide();
	}
}

// Hides the panel
void PlanetPanel::hide()
{
	ImagePanel::hide();
	_icon.hide();
	factoryButton.hide();
	minesButton.hide();
	spaceships1Button.hide();
	spaceships2Button.hide();
	spaceships3Button.hide();
	spaceships4Button.hide();

	//prevent updating of panel when invisible
	_observed = 0;
}

void PlanetPanel::on_clicked(Button *clickedButton) 
{
	Planet* planet = dynamic_cast<Planet*>(_observed);

	if(planet==0) return;
	
	if(clickedButton==&factoryButton) {
		planet->buildFactory(100);
	}
	else if(clickedButton==&minesButton) {
		planet->buildMine(100);
	}
	else if(clickedButton==&spaceships1Button) {
		planet->orderSpaceship(1,ST_YATAI_VIII);
	}
	else if(clickedButton==&spaceships2Button) {
		planet->orderSpaceship(1,ST_JONTH);
	}
	else if(clickedButton==&spaceships3Button) {
		planet->orderSpaceship(1,ST_CYSUS);
	}
	else if(clickedButton==&spaceships4Button) {
		planet->orderSpaceship(1,ST_METHI);
	}
}